// TicketRecordsActivity.java
package com.example.finalhomework_lienyu;

import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.content.Context;

import androidx.appcompat.app.AppCompatActivity;

import com.example.finalhomework_lienyu.util_classes.SQLiteDataBaseHelper;

import java.util.ArrayList;

public class TicketRecordsActivity extends AppCompatActivity {
    private SQLiteDataBaseHelper dbHelper;
    private ListView listViewTicketRecords;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ticket_records);

        dbHelper = new SQLiteDataBaseHelper(this);
        listViewTicketRecords = findViewById(R.id.listView_ticket_records);

        loadTicketRecords();
    }

    private void loadTicketRecords() {
        SharedPreferences sharedPreferences = getSharedPreferences("user_prefs", Context.MODE_PRIVATE);
        String userId = sharedPreferences.getString("user_account", "default_user");

        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(SQLiteDataBaseHelper.TABLE_TICKETS, null, null, null, null, null, null);

        ArrayList<String> records = new ArrayList<>();
        if (cursor != null && cursor.moveToFirst()) {
            do {
                String date = cursor.getString(cursor.getColumnIndex(SQLiteDataBaseHelper.COLUMN_DATE));
                String time = cursor.getString(cursor.getColumnIndex(SQLiteDataBaseHelper.COLUMN_TIME));
                String category = cursor.getString(cursor.getColumnIndex(SQLiteDataBaseHelper.COLUMN_CATEGORY));
                int number = cursor.getInt(cursor.getColumnIndex(SQLiteDataBaseHelper.COLUMN_NUMBER));
                records.add("日期: " + date + ", 时间: " + time + ", 类型: " + category + ", 数量: " + number);
            } while (cursor.moveToNext());
            cursor.close();
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, records);
        listViewTicketRecords.setAdapter(adapter);
    }
}